﻿/// <reference name="MicrosoftAjax.debug.js" />
/// <reference path="AjaxAction.js" />

function select(tr) {
    var selector = getElementsByClassName("selector", "input", tr)[0];
    selector.checked = !selector.checked;
}

function up(url) {
    var backlog = $get('backlogTable');
    var items = getSelectedBacklogItems(backlog);
    while(items.length > 0) {
        var rowIndex = items.shift();
        if (rowIndex > 1) {
            backlog.moveRow(rowIndex, rowIndex-1);
            var selector = getElementsByClassName("selector", "input", backlog.rows[rowIndex-1])[0];
            selector.checked = true;        
            var priority = getElementsByClassName("priority", "span", backlog.rows[rowIndex-1])[0];
            setStringValue(priority, '##');
        }
    }    
}

function down(url) {
    var backlog = $get('backlogTable');
    var items = getSelectedBacklogItems(backlog);
    while(items.length > 0) {
        var rowIndex = items.pop();
        if (rowIndex < backlog.rows.length-1) {
            backlog.moveRow(rowIndex, rowIndex+1);                  
            var selector = getElementsByClassName("selector", "input", backlog.rows[rowIndex+1])[0];                          
            selector.checked = true;        
            var priority = getElementsByClassName("priority", "span", backlog.rows[rowIndex+1])[0];
            setStringValue(priority, '##');
            
        }
    }    
}

function getSelectedBacklogItems(table) 
{   
    var selectors = getElementsByClassName("selector", "input", table);
    var arr = [];
    for( var i=0; i<selectors.length; i++){        
        if (selectors[i].checked) {
            var tr = findParentNode(selectors[i], "tr");
            if (tr.rowIndex > 0) { 
                arr.push(tr.rowIndex);
            }            
        }        
    }
    return arr;
}

function setPriorities(url) {
    var sb = new Sys.StringBuilder();
    var backlog = $get('backlogTable');
    var ids = getElementsByClassName("id", "input", backlog);
    for (var i = 0; i < ids.length; i++) {
        var element = ids[i];
        var name = "requestIds";
        sb.append(name);
        sb.append('=');
        sb.append(encodeURIComponent(element.value));
        sb.append('&');        
     }
    var options = {url: url, update: 'backlog' };    
    Sys.UI.DomElement.removeCssClass($get("progressBar"), "off");
    new HttpHelper.AjaxAction().invoke(options, sb.toString(), onRequestFinished);        
}

function onRequestFinished(executor, eventArgs) {
    Sys.UI.DomElement.addCssClass($get("progressBar"), "off");
}

function getStringValue(el) {
    if (el.tagName == "INPUT") {
        return trim(el.value);
    } else {
        return trim(el.firstChild.nodeValue);
    }
}

function setStringValue(el, value) {
    if (el.tagName == "INPUT") {
        el.value = value;
    } else {
        el.firstChild.nodeValue = value;
    }
}

// helper functions
function trim(str) {
    return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}
function findParentNode(el, nodeType) {
    nodeType = nodeType.toUpperCase();
    while(el != null) {
        el = el.parentNode;
        if(el.nodeName.toUpperCase() == nodeType) break;
    }
    return el;
}

function getElementsByClassName(strClass, strTag, objContElm) {
  strTag = strTag || "*";
  objContElm = objContElm || document;
  var objColl = objContElm.getElementsByTagName(strTag);
  if (!objColl.length &&  strTag == "*" &&  objContElm.all) objColl = objContElm.all;
  var arr = new Array();
  var delim = strClass.indexOf('|') != -1  ? '|' : ' ';
  var arrClass = strClass.split(delim);
  for (var i = 0, j = objColl.length; i < j; i++) {
    var arrObjClass = objColl[i].className.split(' ');
    if (delim == ' ' && arrClass.length > arrObjClass.length) continue;
    var c = 0;
    comparisonLoop:
    for (var k = 0, l = arrObjClass.length; k < l; k++) {
      for (var m = 0, n = arrClass.length; m < n; m++) {
        if (arrClass[m] == arrObjClass[k]) c++;
        if (( delim == '|' && c == 1) || (delim == ' ' && c == arrClass.length)) {
          arr.push(objColl[i]);
          break comparisonLoop;
        }
      }
    }
  }
  return arr;
}


if(typeof document.getElementsByTagName("TABLE")[0].moveRow == "undefined"){
    Node.prototype.moveRow = function(){
    if(this && this.nodeName.match(/^(table|t(body|head|foot))$/i)){
        try {
            one = (!arguments[0] && arguments[0] != 0?-1:arguments[0]);
            two = (!arguments[1] && arguments[1] != 0?-1:arguments[1]);
            // Makes sure the row exists and then makes sure the insertable row isn't greater then the length
            if(!this.rows[one] || two > this.rows.length){
                var err = new Error();
                throw err;
            }
            // This is just so that it gets put in the right place.
            if(two > one)
                two = two+1;
            else if(one > two)
                one = one+1;
            
            newRow = this.insertRow(two);
            newRow.innerHTML = this.rows[one].innerHTML;
            newRow.className = this.rows[one].className;
            this.deleteRow(one);
        } catch(e) {
    }
}
}
}